﻿$(function () {
    $(".userpanel .menu .menu-list > li:eq(0)").addClass("sel");
    layui.use(["form","laypage"], function () {
        var form = layui.form;
        var laypage = layui.laypage;
        form.verify({
            confirmPwd: function (value) {
                if ($('input[name=newpwd]').val() !== value)
                    return '两次密码输入不一致！';
            },
            password: function (value) {
                var reg = /^\w{6,16}$/;
                if (!reg.test(value))
                    return "密码必须是6~16位的字母或数字!";
            },
            unequalToOldPwd: function (value) {
                var old = $("input[name='oldpwd']").val();
                if (value === old) return "新密码不能与原始密码相同!";
            }
        });

        let nowcurr = 0;
        let poption = {
            elem: "collect-page",
            limit: 8,
            layout: ['prev', 'page', 'next', 'count'],
            jump(obj, isfirst) {
                let index = obj.curr;
                if (nowcurr == index) return;
                loadPage(index);
            }
        }
        laypage.render(poption);

        function loadPage(index) {
            $.get({
                url: "/userinfo/PartialFavorites",
                data: { pindex: index },
                dataType: "html",
                success: function (html) {
                    var dom = $(".uif .page-fav .favlist");
                    dom.html(html);
                    let datalen = parseInt($(".uif .panel-rig .sclist").attr("data-datalen"));
                    poption.count = datalen;
                    nowcurr = index;
                    poption.curr = index;
                    laypage.render(poption);
                    bindCircleProgbar();
                    bindCancelFav();
                }
            })
        }

        function bindCancelFav() {
            $(".uif .page-fav .btn-cstar").on("click", function () {
                let $tis = $(this);
                var gid = parseInt($tis.attr("data-gid"))
                $tis.parents("li").first().remove();
                $.post({
                    url: "/userinfo/cancelcollect",
                    data: { gid: gid },
                    dataType: "json",
                    success(res) {
                        if (res.Isucc) {
                            layer.msg("取消收藏成功", { time: 1000 });
                            loadPage(nowcurr);
                        }
                    }
                })
            })
        }
    });

   

    var syanime = {
        targets: ".uif .panel-rig .tab-content .other .cenbox > .item",
        scale: [0, 1],
        duration: 1500,
        delay: anime.stagger(300)
    };
    anime(syanime);

    $(".tab-title > li").on("click", function () {
        var idx = $(this).index();
        $(".tab-title > li").removeClass("this");
        $(this).addClass("this");
        if (idx == 0) anime(syanime);
        $(".tab-content .tab-page").removeClass("show");
        $(".tab-content .tab-page:eq(" + idx + ")").addClass("show");
    });

    var ischange = false;
    $("#editdata").on("click", function () {
        layer.open({
            title: "&#xe68b; 编辑信息",
            content: $("#editbx").show(),
            type: 1,
            resize: false,
            area: ["470px", "480px"],
            end: function () {
                $("#editbx").hide();
                if (ischange) location.reload();
            }
        });
    });

    $("#alterpwd").on("click", function () {
        layer.open({
            title: "&#xe798; 修改密码",
            content: $("#alterpwdbx").show(),
            type: 1,
            resize: false,
            area: ["400px", "350px"],
            end: function () {
                $("#alterpwdbx").hide();
            }
        })
    })

    $(".uif .userblk .headimg").on("click", function () {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            area: ['500px', "600px"],
            shadeClose: true,
            content: `<div style="text-align:center;height:100%;"><img src="${$(this).attr("src")}"  style="height:100%;" /></div>`
        });
    });

    $("#headimgul").on("change", function () {
        if (this.files.length > 0) {
            let file = this.files[0];
            let reader = new FileReader();
            reader.onload = function () {
                // 当 FileReader 读取文件时候，读取的结果会放在 FileReader.result 属性中
                document.querySelector('#headimgshow').src = this.result;
            };
            reader.readAsDataURL(file);
        }
    });

    $("#btn-change-mob").on("click", function () {
        layer.open({
            title: '修改手机号',
            type: 1,
            resize: false,
            content: $("#mob-changebx"),
            area: ["400px", "240px"],
            success: function () {
                $("#mob-changebx").show();
            },
            end: function () {
                $("#mob-changebx").hide()
                $(".stepdev .long").removeAttr("style");
            }
        });
    });

    $("#btn-change-email").on("click", function () {
        layer.open({
            title: '绑定邮箱',
            type: 1,
            resize: false,
            content: $("#email-changebx"),
            area: ["400px", "240px"],
            success: function () {
                $("#email-changebx").show();
            },
            end: function () {
                $("#email-changebx").hide();
                $(".stepdev .long").removeAttr("style");
            }
        })
    });

    $(".step-next-mob").on("click", function () {
        var yz = /^\d{11}$/;
        var s = $("#newmob").val();
        if (!yz.test(s)) {
            layer.msg('手机号格式不正确!', {
                icon: 2,
                anim: 6,
                time: 2000
            });
            return;
        }
        $(".stepdev .long").css("margin-left", "-400px");
    });

    $(".step-next-email").on("click", function () {
        var yz = /^\w+@\w+\.com$/;
        var s = $("#newemail").val();
        if (!yz.test(s)) {
            layer.msg('邮箱格式不正确!', {
                icon: 2,
                anim: 6,
                time: 2000
            });
            return;
        }
        $(".stepdev .long").css("margin-left", "-400px");
    });

    $("#formuser").ajaxForm({
        dataType: "json",
        success: function (data) {
            if (data.Isucc) {
                ischange = true;
                layer.msg("修改成功!", {
                    icon: 1,
                    time: 1000,
                    end: function () {
                        layer.closeAll('page');
                    }
                });
            } else {
                switch (data.ErrorCode) {
                    case 1:
                        layer.msg("头像大小不能超过1MB", { icon: 2 });
                        break;
                    default:
                        layer.msg("修改失败!", { icon: 2 });
                        break;
                }
            }
        }
    })

    $("#form-alterpwd").ajaxForm({
        dataType: "json",
        success: function (result) {
            if (result.Isucc) {
                layer.msg("密码修改成功!",
                    {
                        time: 800,
                        end: function () {
                            $("#form-alterpwd")[0].reset();
                            layer.closeAll();
                        }
                    })
            } else {
                var code = result.ErrorCode;
                layer.msg(code == 2 ? "原始密码输入错误!" : "密码修改失败!", {
                    icon: 5,
                    time: 1000
                });
            }
        }
    })
});